﻿using System;
using System.Web.UI.WebControls;

namespace Medianamik.UI.Controls
{
    /// <summary>
    /// TextBox régulier
    /// </summary>
    public class SingleLineTextBox : BaseMedianamikControl
    {
        protected TextBox _textbox;
        protected TextBoxMode Mode { get; set; }

        public SingleLineTextBox()
        {
            Mode = TextBoxMode.SingleLine;
            IsMultiLingual = true;
        }

        protected override void CreateControlHierarchy(bool pUseDataSource)
        {
            _textbox = new TextBox();
            Controls.Add(_textbox);
            _textbox.ID = ControlToValidateId;

            if (pUseDataSource)
            {
                _textbox.Text = DataSource == null ? string.Empty : DataSource.ToString();

                switch (Mode)
                {
                    case TextBoxMode.SingleLine:
                        _textbox.CssClass = "TxtBox";
                        break;
                    case TextBoxMode.MultiLine:
                        _textbox.CssClass = "TxtArea";
                        break;
                    case TextBoxMode.Password:
                        _textbox.CssClass = "TxtPassword";
                        break;
                }

                _textbox.TextMode = Mode;
                _textbox.Width = ControlInfo.Width.HasValue ? ControlInfo.Width.Value : WIDTH;

                if (Mode == TextBoxMode.MultiLine)
                {
                    _textbox.Height = ControlInfo.Height.HasValue ? ControlInfo.Height.Value : HEIGHT;
                }
                else
                {
                    if (MaxLength.HasValue)
                        _textbox.MaxLength = MaxLength.Value;
                }
            }
        }

        public override void SetValue(object value)
        {
            EnsureChildControls();
            if (value != null)
                _textbox.Text = value.ToString();
            else
                _textbox.Text = string.Empty;
        }

        public override Type TypeOfValue
        {
            get { return typeof(string); }
        }

        public override object GetValue()
        {
            EnsureChildControls();
            return _textbox.Text;
        }
    }
}